package objectClasses;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.security.*;


public abstract class User
{
	final static String USERFILE = "User.txt";
	final static String ATTENDEE = "Attendee.txt";
	final static String USERCATS = "UserCats";
	
	//Encrypts the password and returns the encryption in bytes.
	private static String encrypt(String input)throws Exception 
	{
		MessageDigest m = MessageDigest.getInstance("MD5");
		m.reset();
		m.update(input.getBytes());
		byte[] digest = m.digest();
		BigInteger bigInt = new BigInteger(1,digest);
		String hashtext = bigInt.toString(16);
		// Now we need to zero pad it if you actually want the full 32 chars.
		while(hashtext.length() < 32 )
		{
		  hashtext = "0"+hashtext;
		}
		return hashtext;
    }
	
	/**
	 * Just send in a username and password and it will return a newly created user
	 * @param user 
	 * @param pass
	 * @return
	 */
	public static UserInfo login(String user, String pass)
	{
	    try 
	    {
			Map<String, String> user_map = readUserFile();
			String encrypted_pass = user_map.get(user);
			char user_type = encrypted_pass.charAt(encrypted_pass.length() - 1);
		    if(encrypt(pass).equals(encrypted_pass))
			{
		    	System.out.print("Login Successful");
		    	String temp[] = null;
		    	temp = getUserInfo(user);
		    	UserInfo ret = new UserInfo(temp[0], temp[1], temp[2], temp[3], temp[4]);
		    	ret.setUserType(user_type);
		    	return ret;
			}  
		} 
	    catch (Exception e) 
	    {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return null;
	}//US15
	
	/**
	 * Use this method to create a new user in the user text file and new user information.
	 * @param user
	 * @param pass
	 * @param email
	 * @param firstname
	 * @param lastname
	 * @param phone
	 * @param user_type
	 */
	public static void Register(String user, String pass, String email, String firstname,
						 String lastname, String phone, char user_type)
	{
	    String registration = user + ":" + email + ":" + firstname + ":" + lastname + ":" + phone;
	    String encrypted_pass = null;
		try 
		{
			encrypted_pass = encrypt(pass);
		} 
		catch (Exception e1) 
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String new_user = user + ":" + encrypted_pass;
	    File file = new File(ATTENDEE);
	    FileWriter writer;
		try 
		{
			writer = new FileWriter(file, true);
			BufferedWriter out = new BufferedWriter(writer);
			out.write(registration);
			out.newLine();
			out.close();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		file = new File(USERFILE);
		try 
		{
			writer = new FileWriter(file, true);
			BufferedWriter out = new BufferedWriter(writer);
			out.write(new_user);
			out.newLine();
			out.close();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	public static String[] getUserInfo(String username) throws IOException
	{
		File file = new File(ATTENDEE);
		String[] splitVals=null;
		if (!file.isFile() && !file.createNewFile())
		{
			throw new IOException("Error creating new file: " + file.getAbsolutePath());
		}
		BufferedReader r = new BufferedReader(new FileReader(file));
		try 
		{
			String line = r.readLine();
			boolean found = false;
			while(line != null && !found)
			{
				splitVals = line.split(":");
				if(splitVals[0].equals(username))
				{
					found = true;
					break;
				}
				System.out.print(splitVals[0]);
				line = r.readLine();
			}
			if(line == null)
			{
				return null;
			}
		}
		finally
		{
			r.close();
		}
		return splitVals;
	}

	public static Map<String,String> readUserFile() throws IOException
	{
		File file = new File(USERFILE);
		String[] splitVals=null;
		String splitter="";
		Map<String, String> user_list = new HashMap<String,String>();
		if (!file.isFile() && !file.createNewFile())
		{
			throw new IOException("Error creating new file: " + file.getAbsolutePath());
		}
		BufferedReader r = new BufferedReader(new FileReader(file));
		try 
		{
			while((splitter = r.readLine()) != null)
			{
				splitVals = splitter.split(":");
				user_list.put(splitVals[0], splitVals[1]);
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			r.close();
		}
		return user_list;
	}
	
}
